<?php
if (!defined('W_ROOT')) {
    exit();
}
$_SESSION['shop_return_url'] = $_SERVER['REQUEST_URI'];
$pharmacy_id = $_SESSION['acc_id'];
$qstring = '';
$rowsPerPage = isset($_GET['num']) ? $_GET['num'] : '10';

if ((isset($_GET['catId']) && (int) $_GET['catId'] > 0)) {
    $changeCate = $_GET['catId'];
    $sql2 = "AND pic.category_id = $changeCate";
    $qstring = "catId=$changeCate";
} else {
    $changeCate = 0;
    $sql2 = '';
    $qstring = '';
}
$user_id = $_SESSION['acc_id'];
$categoryList = buildCategoryOptions($changeCate);
$sql = "SELECT p.*,pip.*,pic.*, COUNT(pip.country_id) AS num_count FROM tblproduct AS p,tblproduct_in_pharmacy AS pip, tblproduct_in_category AS pic WHERE p.id = pip.product_id AND pip.pharmacy_id = $pharmacy_id AND pic.product_id = pip.product_id $sql2 GROUP BY pip.product_id ORDER BY pip.pid DESC";
$result = mysql_query(getPagingQuery($sql, $rowsPerPage));
$pagingLink = getPagingLink($sql, $rowsPerPage);
$row_num = mysql_num_rows($result);
$countproduct = mysql_query($sql);

$reachedPost = user_reached_post();
require_once(S_ROOT.'/confirmDelete.php');

?>
<div class="breadcrumb">
    <a class="lever_1" style="z-index:100" href="<?php echo base_url;?>"><?php echo $lang['HOME'];?></a>
    <a class="lever_2" style="z-index:99" href="<?php echo base_url;?>account"><?php echo $lang['SELL_DASH'];?></a>
    <a class="lever_3" style="z-index:98" href="<?php echo base_url;?>account/product"><?php echo $lang['MANAG_PRO'];?></a>
</div>
<?php if(!empty($_GET['success'])){?>
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert">&times;</button>
 Product has been added | updated!
</div>
<?php }?>
<?php if(!empty($_GET['message'])){?>
<div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">&times;</button>
 Product has been deleted!
</div>
<?php }?>
<div id="content" class="page">
    <h1 class="page-title"><?php echo $lang['SELL_ART_MANAGE'];?></h1>
        <form action="<?php echo base_url; ?>account/product/process.php" method="post" name="frmlist">
               
                <div style="float:right; padding-top:15px;">
                    <input class="button" type="button" name="btnadd" value="Add New" <?php if(empty($reachedPost)):?>onClick="javascript:window.location.href = '<?php echo base_url; ?>account/product/index.php?view=add&amp';
        return false;"<?php else: echo ' disabled="disabled" style="background:#C3C5BF!important;cursor: not-allowed;border: 1px solid #ADADAD;"'; endif;?> />&nbsp;&nbsp;
                    <input class="button" type="button" name="btnpublish" value="Publish" onclick="publishSelected(document.frmlist, '<?php echo htmlspecialchars(getAllGetParams(array('action', 'view'))); ?>', 'No item is selected for Publishing!', 1, 'Publish(s)', '<?php echo base_url; ?>account/product/process')" />&nbsp;&nbsp;
                    <input class="button" type="button" name="btnunpublish" value="Unpublish" onclick="publishSelected(document.frmlist, '<?php echo htmlspecialchars(getAllGetParams(array('action', 'view'))); ?>', 'No item is selected for Unpublishing!', 0, 'Publish(s)', '<?php echo base_url; ?>account/product/process')" />
                    <input class="button" type="button" name="btndelete" value="Delete" onclick="deleteSelected(document.frmlist, '<?php echo htmlspecialchars(getAllGetParams(array('action', 'view'))); ?>', 'No item is selected for Delete!', '<?php echo base_url; ?>account/product/process', 'delete(s)')" />
                </div>
            <div style="clear:both;">&nbsp;</div>
            <div class="box">
                <div class="center_admin">
                    <table cellpadding="0" cellspacing="0" border="0" width="100%" class="table table-striped table-hover">
                        <tbody>
                                <tr>
                                    <td align="center" style="width: 20px!important;">
                                        <input type="checkbox" name="chkall" id="chkall" onClick="checkAll(this.form, this.checked)" style="cursor:pointer;"></td>
                                    <td align="center" style="width: 75px;"><?php echo $lang['IMG']; ?></td>
                                    <td align="left" style="padding-left:15px;"><?php echo $lang['PRO_NAME']; ?></td>
                                    <td style="width: 150px;"><?php echo $lang['CON_PRICE']; ?></th>
                                    <td style="width: 75px;"><?php echo $lang['STATUS']; ?></td>
                                    <td width="12%"><?php echo $lang['ACTION']; ?></td>
                                </tr>
                            <?php
                            $i = 1;
                            if (dbNumRows($result) > 0) {
                                while ($row = mysql_fetch_assoc($result)) {
                                    extract($row);

                                    if ($i % 2) {
                                        $class = 'row1';
                                    } else {
                                        $class = 'row2';
                                    }

                                    $sql_img = "SELECT images FROM tblimage WHERE product_id = $id";
                                    $result_img = mysql_query($sql_img);
                                    $row_img = mysql_fetch_assoc($result_img);

                                        $pathImage = S_S_IMAGES_DIR;
                                        $pathImages = base_url . 'product/images/';
                                        if ($row_img['images'] && file_exists($pathImage . $row_img['images'])) {
                                            $thumbnail_w = $pathImages . 'thumb/' . $row_img['images'];
                                            $thumbnail_s = $pathImages . 'thumb/' . $row_img['images'];
                                        } else {
                                            $thumbnail_w = 'images/no-image-small.png';
                                            $thumbnail_s = 'images/no-image-small.png';
                                        }
                                    //Resample original image to display as Thumtail

                                    $country = mysql_query("SELECT COUNT(DISTINCT country_id) AS pid
		FROM tblproduct_in_pharmacy WHERE product_id=" . $id . " AND pharmacy_id=" . $pharmacy_id);

                                    $price = mysql_query("SELECT price FROM tblproduct_in_pharmacy WHERE product_id=" . $id . " AND pharmacy_id=" . $pharmacy_id . " ORDER BY price ASC LIMIT 0,1");
                                    $myprice = mysql_fetch_assoc($price);
                                    $price1 = mysql_query("SELECT price FROM tblproduct_in_pharmacy WHERE product_id=" . $id . " AND pharmacy_id=" . $pharmacy_id . " ORDER BY price DESC LIMIT 0,1");
                                    $myprice1 = mysql_fetch_assoc($price1);
                                    ?>
                                    <tr>
                                        <td align="center"><input type="checkbox" name="id[]" value="<?php echo $id; ?>"/></td>
                                        <td align="center"><img src="<?php echo $thumbnail_w; ?>" width="75" height="75" border="0" /></td>
                                        <td align="left">
                                            <strong style="color:#06C;"><?php echo stripslashes($product_name) ?></strong>        
                                        </td>
                                        <td width="19%" align="center">
                                            <strong style="color:#F60">
                                                <?php
                                                if ($myprice['price'] == $myprice1['price']) {
                                                    echo '$' . $myprice['price'];
                                                } else {
                                                    echo '$' . $myprice['price'] . ' - $' . $myprice1['price'];
                                                }
                                                ?>
                                            </strong>
                                        </td>
                                        <td width="10%" align="center"><?php echo stateProduct($id, $pharmacy_id, $status) ?></td>
                                        <td width="12%" align="center">
        <?php
        if ($owner == $pharmacy_id) {
            $edit = $id;
        } else {
            $edit = $id . '&owner=0';
        }
        ?>
                                            <div class="btn-group">
                                                <a class="btn btn-primary btn-inverse"  href="<?php echo base_url; ?>account/product/index.php?view=edit&id=<?php echo $edit; ?>"><i class="icon-pencil icon-white"></i></a>
                                                <a class="btn btn-primary btn btn-danger" onclick="return ConfirmDelete('<?php echo base_url; ?>/account/product/index.php?view=delete&id=<?php echo $id ?>')" href="javascript:;"><i class="icon-trash icon-white"></i></a>
                                            </div>
                                        </td>
                                    </tr>        
        <?php
        $i++;
    }// end while
} else {
    ?>
                                <tr>
                                    <td colspan="6" style="height:75px;" align="center"><strong>You don't have any product yet! Please add new product</strong></td>
                                </tr>
                                <?php
                            }
                            ?> 
                        </tbody>       
                    </table>
                    <style>.compare-info td{width: 20px;}</style>
                </div>
            </div><!--<div class="box">-->
            <div class="row-fluid">
                <div class="span7">
                    <div class="wp-pagenavi">
<?php echo $pagingLink; ?>
                        <div class="clear"></div>
                    </div>
                </div>
                <div class="span3 offset2">
                    view per page
                    <select class="right span4" name="numpage" onchange="javascript:viewByNumPage();">
                        <option value="10" <?php echo($rowsPerPage == 10) ? ' selected="selected"' : ''; ?>>10</option>
                        <option value="20" <?php echo($rowsPerPage == 20) ? ' selected="selected"' : ''; ?>>20</option>
                        <option value="30" <?php echo($rowsPerPage == 30) ? ' selected="selected"' : ''; ?>>30</option>
                        <option value="40" <?php echo($rowsPerPage == 40) ? ' selected="selected"' : ''; ?>>40</option>                                
                        <option value="50" <?php echo($rowsPerPage == 50) ? ' selected="selected"' : ''; ?>>50</option>        
                    </select>
                </div>            
            </div> 
        </form>
</div><!--<div class="row-fluid">-->

<div id="dialogDelete" title="Are you sure do you want to delete?"
	style="display: none;">
	<p>Are you sure do you want to delete?</p>
</div>
<div id="dialogAlert" title="Cancel" style="display: none;">
	<p>Please check at least!</p>
        
        <style>
            
            .ui-widget {
    font-family: Verdana,Arial,sans-serif;
    font-size: .8em;
}

.ui-widget-content {
    background: #F9F9F9;
    border: 1px solid #90d93f;
    color: #222222;
}

.ui-dialog {
    left: 0;
    outline: 0 none;
    padding: 0 !important;
    position: absolute;
    top: 0;
}

#success {
    padding: 0;
    margin: 0; 
}

.ui-dialog .ui-dialog-content {
    background: none repeat scroll 0 0 transparent;
    border: 0 none;
    overflow: auto;
    position: relative;
    padding: 0 !important;
}

.ui-widget-header {
    background: #D90000;
    border: 1px solid #FF0000;
    border: 0;
    color: #fff;
    font-weight: normal;
}

.ui-dialog .ui-dialog-titlebar {
    padding: 0.1em .5em;
    position: relative;
        font-size: 1em;
}
.ui-dialog button.bDefault {background: #EEEEEE;color:#ccc;}
.ui-dialog button.bRed {background: #ff0000;color:#fff;}
        </style>
</div>